import { _decorator, Component, Node, v2, PhysicsSystem2D, ERaycast2DType, UITransform } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('NewComponent')
export class NewComponent extends Component {
	dir = v2(0,1)
	onLoad(){
		PhysicsSystem2D.instance.enable = true
	}
    start() {
		console.debug(this.node.getPosition())
    }

    update(deltaTime: number) {
        this.node.setPosition(
			this.node.getPosition().x,
			this.node.getPosition().y+this.dir.y*100*deltaTime,
		)
		// let ray = PhysicsSystem2D.instance.raycast(
		// 	this.node.getPosition(),
		// 	v2(this.node.getPosition().x, this.node.getPosition().y+this.dir.y * 200)
		// 	// v2(this.node.getPosition().x, this.node.getPosition().y + this.dir.y*100)
		// )
		let phy = PhysicsSystem2D.instance
		//转换为世界坐标系
		let newp = this.node.getParent().getComponent(UITransform).convertToWorldSpaceAR(this.node.getPosition())
		//然后就能检测到了
		let res = phy.raycast(newp, v2(newp.x, newp.y + this.dir.y*200))
		if(res.length>0){
			this.dir.y*=-1
		}
    }
}

